package com.hqhy.like.admin.account.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hqhy.like.admin.account.pojo.param.UserAddNewParam;
import com.hqhy.like.admin.account.pojo.param.UserPwdPhoneUpdateParam;
import com.hqhy.like.admin.account.pojo.param.UserSearchParam;
import com.hqhy.like.admin.account.pojo.param.UserUpdateInfoParam;
import com.hqhy.like.admin.account.pojo.vo.UserListItemVO;
import com.hqhy.like.admin.account.pojo.vo.UserStandardVO;
import com.hqhy.like.admin.account.service.IUserService;
import com.hqhy.like.common.consts.web.HttpConsts;
import com.hqhy.like.common.pojo.vo.PageData;
import com.hqhy.like.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * 处理用户相关请求的控制器类
 */
@Slf4j
@RestController
@RequestMapping("/users")
@Validated
@Api(tags = "1.员工管理")
public class UserController implements HttpConsts {

    @Autowired
    private IUserService userService;

    public UserController() {
        log.info("创建控制器对象：UserController");
    }

    //    @PreAuthorize("hasAuthority('/account/user/add-new')")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid UserAddNewParam userAddNewParam){
        log.debug("开始处理【添加用户】的请求，参数：{}", userAddNewParam);
        userService.addNew(userAddNewParam);
        return JsonResult.ok();
    }

    //    @PreAuthorize("hasAuthority('/account/user/delete')")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "long")})
    public JsonResult delete(@PathVariable @Range(min = 1,message = "请提交合法的用户ID值!") Long id){
        log.debug("开始处理【根据ID删除用户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    //    @PreAuthorize("hasAuthority('/account/user/updateUserAll')")
    @ApiOperation("根据ID修改用户信息")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult updateUserInfo(@PathVariable @Range(min = 1,message = "请提交合法的用户ID值!") Long id,
                                     @Valid UserUpdateInfoParam userUpdateInfoParam){
        log.debug("开始处理【根据ID修改用户】的请求，ID值：{}，参数：{}", id, userUpdateInfoParam);
        userService.updateUserInfo(id, userUpdateInfoParam);
        return JsonResult.ok();
    }

    //    @PreAuthorize("hasAuthority('/account/user/updateUser')")
    @ApiOperation("修改个人信息")
    @ApiOperationSupport(order = 310)
    @PostMapping("/userInfo/update")
    public JsonResult updatePassword(UserPwdPhoneUpdateParam userPwdPhoneUpdateParam){
        log.debug("开始处理【修改个人信息】的请求，参数：{}", userPwdPhoneUpdateParam);
        userService.updateLoginUser(userPwdPhoneUpdateParam);
        return JsonResult.ok();
    }

    //    @PreAuthorize("hasAuthority('/account/user/updateAvatar')")
    @ApiOperation("修改头像")
    @ApiOperationSupport(order = 320)
    @PostMapping("/{id:[0-9]+}/avatar/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "avatar", value = "新头像的路径", required = true, paramType = "query")})
    public JsonResult updateAvatar(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值!") Long id,
                                   @NotBlank(message = "请提交新头像的路径") String avatar){
        log.debug("开始处理【修改头像】的请求，用户：{}，新头像：{}", id, avatar);
        userService.updateAvatar(id, avatar);
        return JsonResult.ok();
    }

    //    @PreAuthorize("hasAuthority('/account/user/enable')")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 340)
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")})
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id){
        log.debug("开始处理【启用用户】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    //    @PreAuthorize("hasAuthority('/account/user/disable')")
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 341)
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")})
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id){
        log.debug("开始处理【禁用用户】的请求，参数：{}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }

    //    @PreAuthorize("hasAuthority('/account/user/selectById')")
    @ApiOperation("根据ID查询用户")
    @ApiOperationSupport(order = 410)
    @GetMapping("/{id:[0-9]+}")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")})
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交合法的用户ID值！") Long id){
        log.debug("开始处理【根据ID查询用户】的请求，参数：{}", id);
        UserStandardVO tag = userService.getStandardById(id);
        return JsonResult.ok(tag);
    }

    //    @PreAuthorize("hasAuthority('/account/user/selectByUsername')")
    @ApiOperation("根据username查询用户")
    @ApiOperationSupport(order = 411)
    @GetMapping("/username/select")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")})
    public JsonResult getStandardByUsername(String username){
        log.debug("开始处理【根据username查询用户】的请求，参数：{}", username);
        UserStandardVO tag = userService.getStandardByUsername(username);
        return JsonResult.ok(tag);
    }

    //    @PreAuthorize("hasAuthority('/account/user/selectByPhone')")
    @ApiOperation("根据phone查询用户")
    @ApiOperationSupport(order = 412)
    @GetMapping("/phone/select")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String")})
    public JsonResult getStandardByPhone(String phone){
        log.debug("开始处理【根据phone查询用户】的请求，参数：{}", phone);
        UserStandardVO tag = userService.getStandardByPhone(phone);
        return JsonResult.ok(tag);
    }

    //    @PreAuthorize("hasAuthority('/account/user/selectUserAll')")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "条数", defaultValue = "10", paramType = "query", dataType = "int")})
    public JsonResult list(Integer page,Integer size){
        log.debug("开始处理【查询用户列表】的请求，页码：{},每页记录数：{}", page,size);
        Integer pageNum = page == null ? 1 : page;
        Integer pageSize = size == null ? 10 : size;
        PageData<UserListItemVO> pageData = userService.list(pageNum,pageSize);
        return JsonResult.ok(pageData);
    }

    //    @PreAuthorize("hasAuthority('/account/user/search')")
    @ApiOperation("搜索用户列表")
    @ApiOperationSupport(order = 430)
    @PostMapping("/search")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userSearchParam", value = "用户参数", paramType = "query", dataType = "UserSearchParam"),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "条数", defaultValue = "10", paramType = "query", dataType = "int")})
    public JsonResult selectUserList(UserSearchParam userSearchParam, Integer page, Integer size){
        log.debug("开始处理【搜索用户列表】的请求，用户参数：{}, 页码：{},每页记录数：{}",userSearchParam, page,size);
        Integer pageNum = page == null ? 1 : page;
        Integer pageSize = size == null ? 10 : size;
        PageData<UserListItemVO> pageData = userService.selectUserList(userSearchParam,pageNum,pageSize);
        return JsonResult.ok(pageData);
    }

}